clear all
/* construct regression table at the DEPARTEMENTAL level */
cd "C:\Users\Public\Documents\ImmPanelRevis19\"
/* share immigrants in CZE */
use "ImmPanelRevis19\Donnees\pit\dep_instru2017.dta", clear
egen ize=group(ze)
egen time=group(an)
xtset ize time
gen fzeimmig=F.zeimmig
keep an ze zeimmig mlsnd zepop dimm fzeimmig
rename ze aze
rename zeimmig azeimmig
rename fzeimmig afzeimmig
rename mlsnd amlsnd 
rename dimm adimm
rename zepop azepop
save tempAZE.dta, replace

/* cd "ImmPanelRevis19\Donnees\select" */

use "ImmPanelRevis19\Donnees\indiv.dta", clear
/***************************************************/
/* Construct the table final.dta for the estimates */
/***************************************************/
/**************************************/
/* Definition des zones geographiques */
/**************************************/
/**************************************/
/* 1) => Merge avec les ZE 			  */
/**************************************/
/* elimine la corse */
drop if dep=="2A" | dep=="2B"

gen cgeo=dep+comt
sort cgeo
/* Paris */
replace cgeo="75056" if dep=="75"
/* Lyon */
replace cgeo="69123" if cgeo=="69381" | cgeo=="69382" | cgeo=="69383" | cgeo=="69384" | cgeo=="69385" | cgeo=="69386" | cgeo=="69387" | cgeo=="69388" | cgeo=="69389" 
/* Marseille */
replace cgeo="13055" if cgeo=="13201" | cgeo=="13202" | cgeo=="13203" | cgeo=="13204" | cgeo=="13205" | cgeo=="13206" | cgeo=="13207" | cgeo=="13208" | cgeo=="13209" | cgeo=="13210" | cgeo=="13211" | cgeo=="13212" | cgeo=="13213" | cgeo=="13214" | cgeo=="13215" | cgeo=="13216"

/* definit les ZE */
joinby cgeo using "ImmPanelRevis19\donnees\cgeo_ze.dta", unm(m)
tab _merge
drop if _merge==1
drop _merge
drop zen

/* save old ze definition for region FE */
gen Xze=ze
/* departemental level */
replace ze=dep

/* foreign born workers */
gen fborn=(depnai=="99")
gen ifborn="N" if fborn==0
replace ifborn="FB" if fborn==1
drop if fborn

/*********************/
/* add task contents */
/*********************/
joinby cs2h using "ImmPanelRevis19\Donnees\Onet\CS2_OnetNorm.dta", unm(m)
tab _merge
drop _merge

save temp.dta, replace

/***************************/
/* Add contemporary groups */
/***************************/
use "ImmPanelRevis19\donnees\ident_groupmw.dta", clear
rename gref an
save gref.dta, replace

use temp.dta, clear
joinby nninouv an using gref.dta, unm(m)
tab _merge
drop _merge

egen time=group(an)
xtset indiv time
compress

save temp.dta, replace

/* define our group of interest age 25-50 */
keep if ((age>=25) & (age<=50))

drop if an==2007
keep nninouv
contract nninouv 
drop _freq
gen treat=1
save treat.dta, replace

/* only keep 'treated' individuals */
use temp.dta, clear
joinby nninouv using treat.dta, unm(m)
drop if missing(treat)
drop treat

/* define valid first year observation */
gen ftreat=((age>=25) & (age<=50)) 

save temp2.dta, replace

/* Panel A : 76-82 */
use temp2.dta, clear
/* Balanced sample: treated in first & second period */
keep if an==1976 | an==1982
keep if (ftreat==1 & an==1976 & !missing(F.an)) | (L.ftreat==1 & an==1982)
tab an
gen nind="A"+nninouv
egen btime=group(an)
save tempA.dta, replace

/* Panel B : 82-91 */
use temp2.dta, clear
/* Balanced sample: treated in first & second period */
keep if an==1982 | an==1991
keep if (ftreat==1 & an==1982 & !missing(F.an)) | (L.ftreat==1 & an==1991)
tab an
gen nind="B"+nninouv
egen btime=group(an)
save tempB.dta, replace

/* Panel C : 91-99 */
use temp2.dta, clear
/* Balanced sample: treated in first & second period */
keep if an==1991 | an==1999
keep if (ftreat==1 & an==1991 & !missing(F.an)) | (L.ftreat==1 & an==1999)
tab an
gen nind="C"+nninouv
egen btime=group(an)
save tempC.dta, replace

/* Panel D : 99-07 */
use temp2.dta, clear
/* Balanced sample: treated in first & second period */
keep if an==1999 | an==2007
keep if (ftreat==1 & an==1999 & !missing(F.an)) | (L.ftreat==1 & an==2007)
tab an
gen nind="D"+nninouv
egen btime=group(an)
save tempD.dta, replace

/* Merge all balanced sample */
use tempA.dta, clear
append using tempB.dta
append using tempC.dta
append using tempD.dta
save temp3.dta, replace

/* keep the original location */
drop if btime==2
keep nind ze
rename ze oze
save oze.dta, replace

use temp3.dta, clear
drop _merge
joinby nind using oze.dta, unm(m)
rename ze aze
rename oze ze
drop _merge

/**********************************************************/
/* Merge with immigration shocks in the original location */
/**********************************************************/
joinby ze an using "ImmPanelRevis19\Donnees\pit\dep_instru2017.dta", unm(m)
tab _merge
drop _merge


/*********************/
/* change in outcome */
/*********************/
egen indiv2=group(nind)
xtset indiv2 btime
sort indiv2 btime

/**************/
/* occupation */
/**************/
/* Changes in task intensity */
gen dzbasman=D.zbasman 
gen dzbascom=D.zbascom 

/* fixed effects */
xi, pre(Y_) i.an*lsconst i.an*lstrad i.an*lsntrad i.an*lzepop

/* location shifter */
gen lshift=(ze!=aze)
joinby aze an using tempAZE.dta, unm(m)
/* change in share of immigrants in the commuting zone */
gen dzeimmig=azeimmig-zeimmig
/* change in average wage in the commuting zone */
gen damlsnd=D.amlsnd
/* change in total population in the commuting zone */
gen dazepop=ln(azepop)-ln(L.azepop)

gen elim=(ze=="9306" | ze=="9109" | ze=="9110" | ze=="9116" | ///
ze=="8307" | ze=="7206" | ze=="5214" | ze=="4101" | ze=="4111" | ze=="8306")
tab elim

/* eliminate small CZ */
drop if ze=="9306" | ze=="9109" | ze=="9110" | ze=="9116" | ze=="8307" | ze=="7206" | ze=="5214" 
drop if ze=="4101" | ze=="4111" | ze=="8306"

/* elimine outliers de l'age */
drop if btime==2 & age<32
drop if btime==2 & age>59

drop if btime==1 & missing(F.age)

/* fixed effects age x an */
xi i.age*i.an , pre(F_)

/* merge technicians & clerks */
gen tecemp2=(tec==1 | emp2==1)

save "ImmPanelRevis19\Donnees\select\dep_finalmw.dta", replace

use "ImmPanelRevis19\Donnees\select\dep_finalmw.dta", clear
keep if homme==1
save "ImmPanelRevis19\Donnees\select\dep_final.dta", replace
use "ImmPanelRevis19\Donnees\select\dep_finalmw.dta", clear
keep if homme==0
save "ImmPanelRevis19\Donnees\select\dep_finalw.dta", replace
